package controller;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.activiti.engine.task.Task;

import wfm.activiti.ActivitiManager;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.query.QueryDao;
import wfm.util.email.Mail;
import wfm.util.email.MailAccounts;

import com.web.util.WebUtils;

@ManagedBean(name = "checkWorkDoneController")
@SessionScoped

public class CheckWorkDoneController {
	@ManagedProperty(value = "#{user}")
	private User user;
	
	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	private String additionalInformation; 
	
	private Ticket ticket=null;
	
	public Ticket getTicket() {
		return ticket;
	}

	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}

	public CheckWorkDoneController(){
		
	}
	
	@PostConstruct
	public void init(){
		ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
	}

	
	public String finishCheckWork(){
		String nextPage = "myTasks";
		String instanceId = wfInfo.getInstanceId();
		String taskId = wfInfo.getTaskId(); 
		
		//Ticket holen updaten und wieder speichern
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(instanceId, "ticket");
		ticket.setRenterInformation(this.additionalInformation);
		
		QueryDao.saveVariableToProcessById(instanceId, "ticket", ticket);
		
		System.out.println("finishCheckWorkDone");

		// Retrieving Activiti engine
		ActivitiManager manager = ActivitiManager.getInstance();

		try {
			manager.getTaskRuntimeService().complete(taskId);

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
		Task task=QueryDao.getNextTaskAndSetAssignee(instanceId, user);  //holt sich den nächsten Task zur Gruppe und setzt Asignee
		
		if(task!=null){
			wfInfo.setTaskId(task.getId());
			nextPage = QueryDao.getFormKey(task.getId());
		}

		return WebUtils.redirect(nextPage);
	}
	
	public String finishCheckWorkWithMail(){		
		//send mail 
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
		
		
		String recipient = ticket.getCustomer().getMailAddress();   //ticket.getCustomer().getMailAddress();
        String subject = "The Work in your property has been done";
        String text = this.additionalInformation; 
 
        try {
			Mail.send(MailAccounts.GMX, recipient, subject, text);
		} catch (Exception e) {
			System.out.println("Problem with sending Mail to: " + recipient);
		}     

        System.out.println("Sended");
		
		
		//finish
		return finishCheckWork();
	}
	
	public String abortCheckWork(){
		return WebUtils.redirect("myTasks");
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public String getAdditionalInformation() {
		return additionalInformation;
	}

	public void setAdditionalInformation(String additionalInformation) {
		this.additionalInformation = additionalInformation;
	}
}
